package org.fuys.coder.domain.config.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.fuys.coder.domain.config.model.req.AppConfigAddVersionReq;
import org.fuys.coder.common.exception.BaseException;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.domain.config.service.BaseConfig;
import org.fuys.coder.infrastructure.dao.config.ActivityConfigMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.common.config
 * @className: CoderActivityConfig
 * @author: WangJing
 * @description: 活动相关配置
 * @date: 2024/6/30 16:57
 * @version: 1.0
 */

@Configuration
@Component(value = "activityConfig")
@TableName(value ="activity_config")
public class CoderActivityConfig extends BaseConfig implements Serializable {
    //签到的积分数量
    private Integer signupPoint;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Resource
    @TableField(exist = false)
    private ActivityConfigMapper activityConfigMapper;

    @PostConstruct
    public void initConfigValue(){
        //默认查找第一配置
        final CoderActivityConfig coderActivityConfig = activityConfigMapper.selectOne(new LambdaQueryWrapper<CoderActivityConfig>()
                .select(CoderActivityConfig::getSignupPoint)
                .eq(CoderActivityConfig::getVersion, 1));
        if(coderActivityConfig==null){
            throw new BaseException("activity相关配置注入异常:数据库中不存在相关数据");
        }
        signupPoint=coderActivityConfig.getSignupPoint();
        version=1;
    }

    @Override
    protected void setConfigDBValue(Object instance,Integer version) throws RuntimeException {
        if(instance instanceof CoderActivityConfig){
            CoderActivityConfig newRecord=(CoderActivityConfig) instance;
            //设置修改的版本号
            newRecord.setVersion(version);
            //更新指定版本号的配置
            activityConfigMapper.updateById(newRecord);
        }else{
            throw new BusinessException("无法同步到数据库 请检查传递的数据格式!-- 来自活动配置");
        }
    }

    @Override
    protected void switchConfigVersion(Integer version) {
        //查找版本号的配置信息
        final CoderActivityConfig coderActivityConfig = activityConfigMapper.selectOne(new LambdaQueryWrapper<CoderActivityConfig>()
                .select(CoderActivityConfig::getSignupPoint).eq(CoderActivityConfig::getVersion, version));
        if(ObjectUtils.isEmpty(coderActivityConfig)){
            throw new BusinessException("没有此版本号的配置文件 请核对之后再次切换 或添加新版本 --来自活动配置");
        }
        //设置当前使用的版本号 以及 具体信息
        this.setVersion(version);
        this.setSignupPoint(coderActivityConfig.getSignupPoint());
    }

    @Override
    protected void addConfigVersion(AppConfigAddVersionReq req) {
        CoderActivityConfig activityConfig=new CoderActivityConfig();
        activityConfig.setSignupPoint(req.getSignupPoint());
        activityConfig.setVersion(req.getVersion());
        activityConfig.setDescription(req.getDesc());
        this.activityConfigMapper.insert(activityConfig);
    }

    @Override
    protected List<? extends BaseConfig> getConfigList() {
        return this.activityConfigMapper.selectList(new LambdaQueryWrapper<>());
    }


    public Integer getSignupPoint() {
        return signupPoint;
    }

    public void setSignupPoint(Integer signupPoint) {
        this.signupPoint = signupPoint;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof CoderActivityConfig)) return false;
        CoderActivityConfig that = (CoderActivityConfig) o;
        return Objects.equals(getSignupPoint(), that.getSignupPoint());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getSignupPoint());
    }

    @Override
    public String toString() {
        return "CoderActivityConfig{" +
                "version=" + version +
                ", signupPoint=" + signupPoint +
                ", activityConfigMapper=" + activityConfigMapper +
                '}';
    }
}
